// 口腔检查表逻辑处理文件
// import { oralConfig } from '../config/oralConfig.js';

/**
 * 计算口腔检查评估结果
 * @param {Object} formData - 表单数据
 * @returns {Object} 评估结果
 */
export function calculateOralAssessment(formData) {
    const findings = {};
    const recommendations = [];
    let overallStatus = 'normal';
    let riskLevel = 'low';

    // 分析牙齿状况
    const teethFindings = analyzeTeethCondition(formData);
    findings.teeth = teethFindings;

    // 分析牙周情况
    const periodontalFindings = analyzePeriodontalCondition(formData);
    findings.periodontal = periodontalFindings;

    // 分析口腔黏膜
    const mucosaFindings = analyzeMucosaCondition(formData);
    findings.mucosa = mucosaFindings;

    // 分析颌面外科情况
    const maxillofacialFindings = analyzeMaxillofacialCondition(formData);
    findings.maxillofacial = maxillofacialFindings;

    // 生成治疗建议
    const treatmentRecommendations = generateTreatmentRecommendations(findings, formData);
    recommendations.push(...treatmentRecommendations);

    // 确定总体健康状况和风险等级
    const overallAssessment = determineOverallStatus(findings);
    overallStatus = overallAssessment.status;
    riskLevel = overallAssessment.riskLevel;

    // 生成复查建议
    const followUpRecommendations = generateFollowUpRecommendations(overallStatus, riskLevel, findings);

    return {
        findings,
        recommendations,
        followUpRecommendations,
        overallStatus,
        riskLevel,
        summary: generateSummary(findings, overallStatus),
        riskFactors: identifyRiskFactors(findings),
        preventiveAdvice: generatePreventiveAdvice(findings, formData)
    };
}

/**
 * 分析牙齿状况
 * @param {Object} formData - 表单数据
 * @returns {Object} 牙齿状况分析结果
 */
function analyzeTeethCondition(formData) {
    const findings = {
        caries: {
            present: formData.caries_present === 'yes',
            severity: [],
            details: formData.caries_details || '',
            riskLevel: 'low'
        },
        defects: {
            present: formData.defect_present === 'yes',
            causes: formData.defect_cause || [],
            details: formData.defect_details || '',
            riskLevel: 'low'
        },
        missing: {
            present: formData.missing_present === 'yes',
            positions: formData.missing_positions || '',
            duration: formData.missing_duration || '',
            riskLevel: 'low'
        }
    };

    // 分析龋齿严重程度
    if (findings.caries.present && formData.caries_severity) {
        findings.caries.severity = Array.isArray(formData.caries_severity) ?
            formData.caries_severity : [formData.caries_severity];

        // 根据龋齿严重程度确定风险等级
        if (findings.caries.severity.includes('pulpitis') || findings.caries.severity.includes('periapical')) {
            findings.caries.riskLevel = 'high';
        } else if (findings.caries.severity.includes('deep')) {
            findings.caries.riskLevel = 'medium';
        } else if (findings.caries.severity.length > 0) {
            findings.caries.riskLevel = 'low';
        }
    }

    // 分析缺损风险
    if (findings.defects.present) {
        const causes = Array.isArray(formData.defect_cause) ? formData.defect_cause : [formData.defect_cause];
        if (causes.includes('trauma')) {
            findings.defects.riskLevel = 'medium';
        } else if (causes.length > 1) {
            findings.defects.riskLevel = 'medium';
        } else {
            findings.defects.riskLevel = 'low';
        }
    }

    // 分析缺失风险
    if (findings.missing.present) {
        if (formData.missing_duration === 'long') {
            findings.missing.riskLevel = 'high';
        } else if (formData.missing_duration === 'medium') {
            findings.missing.riskLevel = 'medium';
        } else {
            findings.missing.riskLevel = 'low';
        }
    }

    return findings;
}

/**
 * 分析牙周情况
 * @param {Object} formData - 表单数据
 * @returns {Object} 牙周状况分析结果
 */
function analyzePeriodontalCondition(formData) {
    const findings = {
        gingival: {
            color: formData.gingival_color || 'normal',
            texture: formData.gingival_texture || 'normal',
            bleeding: formData.gingival_bleeding || 'none',
            abnormalDesc: formData.gingival_abnormal_desc || '',
            status: 'healthy',
            riskLevel: 'low'
        },
        pockets: {
            normal: formData.pocket_depth_normal === 'normal',
            details: formData.pocket_depth_details || '',
            maxDepth: parseFloat(formData.pocket_depth_max) || 0,
            riskLevel: 'low'
        },
        bone: {
            resorption: formData.bone_resorption || 'none',
            sites: formData.bone_resorption_sites || '',
            riskLevel: 'low'
        }
    };

    // 分析牙龈健康状况
    if (findings.gingival.color !== 'normal' ||
        findings.gingival.texture !== 'normal' ||
        findings.gingival.bleeding !== 'none') {
        findings.gingival.status = 'gingivitis';

        if (findings.gingival.bleeding === 'spontaneous' ||
            findings.gingival.texture === 'swollen') {
            findings.gingival.riskLevel = 'high';
        } else if (findings.gingival.bleeding === 'probing' ||
            findings.gingival.color === 'red') {
            findings.gingival.riskLevel = 'medium';
        } else {
            findings.gingival.riskLevel = 'low';
        }
    }

    // 分析牙周袋深度
    if (!findings.pockets.normal) {
        if (findings.pockets.maxDepth >= 6) {
            findings.pockets.riskLevel = 'high';
        } else if (findings.pockets.maxDepth >= 4) {
            findings.pockets.riskLevel = 'medium';
        } else {
            findings.pockets.riskLevel = 'low';
        }
    }

    // 分析牙槽骨吸收
    if (findings.bone.resorption !== 'none') {
        switch (findings.bone.resorption) {
            case 'severe':
                findings.bone.riskLevel = 'high';
                break;
            case 'moderate':
                findings.bone.riskLevel = 'medium';
                break;
            case 'mild':
                findings.bone.riskLevel = 'low';
                break;
        }
    }

    return findings;
}

/**
 * 分析口腔黏膜情况
 * @param {Object} formData - 表单数据
 * @returns {Object} 黏膜状况分析结果
 */
function analyzeMucosaCondition(formData) {
    const findings = {
        normal: formData.mucosa_normal === 'normal',
        lesionTypes: formData.mucosa_lesion_type || [],
        details: formData.mucosa_lesion_details || '',
        riskLevel: 'low'
    };

    if (!findings.normal && findings.lesionTypes.length > 0) {
        const lesionTypes = Array.isArray(findings.lesionTypes) ?
            findings.lesionTypes : [findings.lesionTypes];

        // 根据病变类型确定风险等级
        if (lesionTypes.includes('mass') || lesionTypes.includes('leukoplakia')) {
            findings.riskLevel = 'high';
        } else if (lesionTypes.includes('erythroplakia') || lesionTypes.length > 2) {
            findings.riskLevel = 'medium';
        } else {
            findings.riskLevel = 'low';
        }
    }

    return findings;
}

/**
 * 分析颌面外科情况
 * @param {Object} formData - 表单数据
 * @returns {Object} 颌面外科分析结果
 */
function analyzeMaxillofacialCondition(formData) {
    const findings = {
        facialSymmetry: {
            normal: formData.facial_symmetry_normal === 'normal',
            description: formData.asymmetry_description || '',
            riskLevel: 'low'
        },
        tmj: {
            clicking: formData.tmj_clicking || 'none',
            pain: formData.tmj_pain || 'none',
            mouthOpening: parseFloat(formData.mouth_opening) || 0,
            otherSymptoms: formData.tmj_other_symptoms || '',
            riskLevel: 'low'
        }
    };

    // 分析面部对称性
    if (!findings.facialSymmetry.normal) {
        findings.facialSymmetry.riskLevel = 'medium';
    }

    // 分析TMJ功能
    let tmjIssues = 0;
    if (findings.tmj.clicking !== 'none') tmjIssues++;
    if (findings.tmj.pain !== 'none') tmjIssues++;
    if (findings.tmj.mouthOpening > 0 && findings.tmj.mouthOpening < 35) tmjIssues++;
    if (findings.tmj.otherSymptoms) tmjIssues++;

    if (tmjIssues >= 3 || findings.tmj.pain === 'severe') {
        findings.tmj.riskLevel = 'high';
    } else if (tmjIssues >= 2 || findings.tmj.clicking === 'severe') {
        findings.tmj.riskLevel = 'medium';
    } else if (tmjIssues >= 1) {
        findings.tmj.riskLevel = 'low';
    }

    return findings;
}

/**
 * 生成治疗建议
 * @param {Object} findings - 检查发现
 * @param {Object} formData - 表单数据
 * @returns {Array} 治疗建议列表
 */
function generateTreatmentRecommendations(findings, formData) {
    const recommendations = [];

    // 牙齿相关建议
    if (findings.teeth.caries.present) {
        const severity = findings.teeth.caries.severity;
        if (severity.includes('periapical')) {
            recommendations.push({
                category: '牙齿治疗',
                priority: 'urgent',
                title: '根尖周炎治疗',
                content: '建议立即进行根管治疗，必要时考虑根尖手术或拔牙',
                type: 'treatment'
            });
        } else if (severity.includes('pulpitis')) {
            recommendations.push({
                category: '牙齿治疗',
                priority: 'high',
                title: '牙髓炎治疗',
                content: '建议尽快进行根管治疗和消炎止痛处理',
                type: 'treatment'
            });
        } else if (severity.includes('deep')) {
            recommendations.push({
                category: '牙齿治疗',
                priority: 'medium',
                title: '深龋治疗',
                content: '建议进行根管治疗或深度充填，必要时冠修复',
                type: 'treatment'
            });
        } else {
            recommendations.push({
                category: '牙齿治疗',
                priority: 'medium',
                title: '龋齿充填',
                content: '建议及时进行补牙治疗，防止龋坏进一步发展',
                type: 'treatment'
            });
        }
    }

    if (findings.teeth.defects.present) {
        recommendations.push({
            category: '牙齿修复',
            priority: 'medium',
            title: '牙齿缺损修复',
            content: '建议进行树脂修复、嵌体修复或冠修复，恢复牙齿形态和功能',
            type: 'treatment'
        });
    }

    if (findings.teeth.missing.present) {
        const duration = formData.missing_duration;
        let priority = 'medium';
        if (duration === 'long') priority = 'high';

        recommendations.push({
            category: '牙齿修复',
            priority: priority,
            title: '缺失牙修复',
            content: '建议考虑种植牙、固定桥或活动义齿修复，恢复咀嚼功能',
            type: 'treatment'
        });
    }

    // 牙周相关建议
    if (findings.periodontal.gingival.status !== 'healthy') {
        if (findings.periodontal.gingival.riskLevel === 'high') {
            recommendations.push({
                category: '牙周治疗',
                priority: 'high',
                title: '牙龈炎症治疗',
                content: '建议立即进行专业洁治和牙周治疗，控制炎症',
                type: 'treatment'
            });
        } else {
            recommendations.push({
                category: '牙周治疗',
                priority: 'medium',
                title: '牙龈护理',
                content: '建议进行龈上洁治和口腔卫生指导',
                type: 'treatment'
            });
        }
    }

    if (!findings.periodontal.pockets.normal) {
        if (findings.periodontal.pockets.riskLevel === 'high') {
            recommendations.push({
                category: '牙周治疗',
                priority: 'high',
                title: '重度牙周炎治疗',
                content: '建议进行牙周手术治疗和系统性牙周维护',
                type: 'treatment'
            });
        } else {
            recommendations.push({
                category: '牙周治疗',
                priority: 'medium',
                title: '牙周刮治',
                content: '建议进行龈下刮治和根面平整术',
                type: 'treatment'
            });
        }
    }

    if (findings.periodontal.bone.resorption !== 'none') {
        recommendations.push({
            category: '牙周治疗',
            priority: 'high',
            title: '牙槽骨吸收处理',
            content: '建议进行牙周再生治疗或维护性治疗',
            type: 'treatment'
        });
    }

    // 黏膜相关建议
    if (!findings.mucosa.normal) {
        if (findings.mucosa.riskLevel === 'high') {
            recommendations.push({
                category: '口腔黏膜',
                priority: 'urgent',
                title: '黏膜病变检查',
                content: '建议立即进行活检和病理检查，排除恶性病变',
                type: 'examination'
            });
        } else {
            recommendations.push({
                category: '口腔黏膜',
                priority: 'medium',
                title: '黏膜病变观察',
                content: '建议定期观察，去除刺激因素，必要时药物治疗',
                type: 'treatment'
            });
        }
    }

    // TMJ相关建议
    if (findings.maxillofacial.tmj.riskLevel !== 'low') {
        if (findings.maxillofacial.tmj.riskLevel === 'high') {
            recommendations.push({
                category: '颞下颌关节',
                priority: 'high',
                title: 'TMJ功能障碍治疗',
                content: '建议进行专业TMJ治疗，可能需要手术干预',
                type: 'treatment'
            });
        } else {
            recommendations.push({
                category: '颞下颌关节',
                priority: 'medium',
                title: 'TMJ保守治疗',
                content: '建议进行理疗、药物治疗或咬合板治疗',
                type: 'treatment'
            });
        }
    }

    return recommendations;
}

/**
 * 确定总体健康状况
 * @param {Object} findings - 检查发现
 * @returns {Object} 总体状况评估
 */
function determineOverallStatus(findings) {
    let highRiskCount = 0;
    let mediumRiskCount = 0;
    let hasAnyIssue = false;

    // 统计各系统的风险等级
    Object.values(findings).forEach(systemFindings => {
        Object.values(systemFindings).forEach(finding => {
            if (finding.riskLevel) {
                hasAnyIssue = true;
                if (finding.riskLevel === 'high') highRiskCount++;
                else if (finding.riskLevel === 'medium') mediumRiskCount++;
            }
        });
    });

    let status = 'excellent';
    let riskLevel = 'low';

    if (highRiskCount >= 2) {
        status = 'poor';
        riskLevel = 'high';
    } else if (highRiskCount >= 1 || mediumRiskCount >= 3) {
        status = 'fair';
        riskLevel = 'medium';
    } else if (mediumRiskCount >= 1 || hasAnyIssue) {
        status = 'good';
        riskLevel = 'low';
    }

    return { status, riskLevel };
}

/**
 * 生成检查总结
 * @param {Object} findings - 检查发现
 * @param {string} overallStatus - 总体状况
 * @returns {string} 检查总结
 */
function generateSummary(findings, overallStatus) {
    const statusMap = {
        excellent: '口腔健康状况良好',
        good: '口腔健康状况基本良好，有轻微问题',
        fair: '口腔健康状况一般，存在需要关注的问题',
        poor: '口腔健康状况较差，需要及时治疗'
    };

    let summary = statusMap[overallStatus] || '口腔健康状况需要进一步评估';

    const issues = [];

    if (findings.teeth.caries.present) {
        issues.push('龋齿');
    }
    if (findings.teeth.defects.present) {
        issues.push('牙齿缺损');
    }
    if (findings.teeth.missing.present) {
        issues.push('牙齿缺失');
    }
    if (findings.periodontal.gingival.status !== 'healthy') {
        issues.push('牙龈炎症');
    }
    if (!findings.periodontal.pockets.normal) {
        issues.push('牙周袋加深');
    }
    if (findings.periodontal.bone.resorption !== 'none') {
        issues.push('牙槽骨吸收');
    }
    if (!findings.mucosa.normal) {
        issues.push('口腔黏膜异常');
    }
    if (findings.maxillofacial.tmj.riskLevel !== 'low') {
        issues.push('颞下颌关节功能异常');
    }

    if (issues.length > 0) {
        summary += `，主要问题包括：${issues.join('、')}。`;
    }

    return summary;
}

/**
 * 识别风险因素
 * @param {Object} findings - 检查发现
 * @returns {Array} 风险因素列表
 */
function identifyRiskFactors(findings) {
    const riskFactors = [];

    if (findings.teeth.caries.present) {
        riskFactors.push({
            factor: '龋齿风险',
            level: findings.teeth.caries.riskLevel,
            description: '存在龋齿，可能与口腔卫生不良、饮食习惯或细菌感染有关'
        });
    }

    if (findings.periodontal.gingival.status !== 'healthy') {
        riskFactors.push({
            factor: '牙周疾病风险',
            level: findings.periodontal.gingival.riskLevel,
            description: '牙龈炎症可能发展为牙周炎，影响牙齿稳定性'
        });
    }

    if (!findings.mucosa.normal && findings.mucosa.riskLevel === 'high') {
        riskFactors.push({
            factor: '口腔癌风险',
            level: 'high',
            description: '口腔黏膜异常病变需要密切关注，排除恶性可能'
        });
    }

    if (findings.teeth.missing.present && findings.teeth.missing.riskLevel === 'high') {
        riskFactors.push({
            factor: '咀嚼功能风险',
            level: 'high',
            description: '长期缺牙可能导致邻牙移位、对颌牙伸长等问题'
        });
    }

    return riskFactors;
}

/**
 * 生成预防建议
 * @param {Object} findings - 检查发现
 * @param {Object} formData - 表单数据
 * @returns {Array} 预防建议列表
 */
function generatePreventiveAdvice(findings, formData) {
    const advice = [];

    // 基础口腔卫生建议
    advice.push({
        category: '日常护理',
        title: '口腔卫生维护',
        content: '每天至少刷牙2次，使用含氟牙膏，配合使用牙线清洁牙缝',
        importance: 'high'
    });

    // 根据年龄给出针对性建议
    if (formData.age && formData.age >= 60) {
        advice.push({
            category: '老年护理',
            title: '老年口腔特殊护理',
            content: '老年人应特别注意口腔卫生，使用软毛牙刷，定期检查义齿适合度',
            importance: 'high'
        });
    } else if (formData.age && formData.age < 18) {
        advice.push({
            category: '青少年护理',
            title: '青少年口腔护理',
            content: '青少年应注意正畸期间的口腔卫生，避免过多甜食摄入',
            importance: 'medium'
        });
    }

    // 根据具体问题给出针对性建议
    if (findings.teeth.caries.present || findings.periodontal.gingival.status !== 'healthy') {
        advice.push({
            category: '饮食建议',
            title: '控制糖分摄入',
            content: '减少甜食和碳酸饮料摄入，餐后及时漱口',
            importance: 'medium'
        });
    }

    if (findings.periodontal.gingival.status !== 'healthy') {
        advice.push({
            category: '专业护理',
            title: '定期洁牙',
            content: '建议每6个月进行一次专业洁牙，维护牙周健康',
            importance: 'high'
        });
    }

    if (findings.maxillofacial.tmj.riskLevel !== 'low') {
        advice.push({
            category: '生活习惯',
            title: '保护颞下颌关节',
            content: '避免咬硬物、单侧咀嚼，减少夜间磨牙',
            importance: 'medium'
        });
    }

    if (findings.mucosa.riskLevel !== 'low') {
        advice.push({
            category: '生活习惯',
            title: '避免刺激因素',
            content: '戒烟限酒，避免辛辣刺激性食物，去除口腔内尖锐边缘',
            importance: 'high'
        });
    }

    // 定期检查建议
    advice.push({
        category: '定期检查',
        title: '口腔健康监测',
        content: '建议每6个月进行一次口腔健康检查，及早发现和处理问题',
        importance: 'high'
    });

    return advice;
}

/**
 * 生成复查建议
 * @param {string} overallStatus - 总体状况
 * @param {string} riskLevel - 风险等级
 * @param {Object} findings - 检查发现
 * @returns {Object} 复查建议
 */
function generateFollowUpRecommendations(overallStatus, riskLevel, findings) {
    let interval = '6个月';
    let urgency = 'routine';
    let specificAdvice = [];

    // 根据风险等级确定复查间隔
    if (riskLevel === 'high') {
        interval = '1-3个月';
        urgency = 'urgent';
    } else if (riskLevel === 'medium') {
        interval = '3-6个月';
        urgency = 'important';
    }

    // 特殊情况的复查建议
    if (findings.mucosa.riskLevel === 'high') {
        specificAdvice.push('黏膜病变需要2周内复查，必要时活检');
    }

    if (findings.teeth.caries.severity &&
        (findings.teeth.caries.severity.includes('pulpitis') ||
            findings.teeth.caries.severity.includes('periapical'))) {
        specificAdvice.push('急性牙髓炎或根尖周炎需要立即治疗');
    }

    if (findings.periodontal.pockets.maxDepth >= 6) {
        specificAdvice.push('重度牙周炎需要1个月内开始系统治疗');
    }

    return {
        interval,
        urgency,
        specificAdvice,
        generalAdvice: '定期口腔检查有助于早期发现和治疗口腔疾病，维护口腔健康'
    };
}

/**
 * 生成口腔检查报告
 * @param {Object} assessmentResult - 评估结果
 * @param {Object} formData - 表单数据
 * @returns {Object} 检查报告
 */
export function generateOralReport(assessmentResult, formData) {
    const { findings, recommendations, followUpRecommendations, overallStatus, riskLevel, summary } = assessmentResult;

    return {
        basicInfo: {
            name: formData.patientName || '未填写',
            age: formData.age || '未填写',
            gender: formData.gender === 'male' ? '男' : formData.gender === 'female' ? '女' : '未填写',
            patientId: formData.patientId || '未填写',
            examDate: formData.examDate ? new Date(formData.examDate).toLocaleString('zh-CN') : '未填写'
        },

        summary: {
            overallStatus,
            riskLevel,
            description: summary
        },

        detailedFindings: {
            teeth: {
                caries: findings.teeth.caries,
                defects: findings.teeth.defects,
                missing: findings.teeth.missing
            },
            periodontal: findings.periodontal,
            mucosa: findings.mucosa,
            maxillofacial: findings.maxillofacial
        },

        recommendations: recommendations.sort((a, b) => {
            const priorityOrder = { urgent: 0, high: 1, medium: 2, low: 3 };
            return priorityOrder[a.priority] - priorityOrder[b.priority];
        }),

        followUpRecommendations,

        riskFactors: assessmentResult.riskFactors,
        preventiveAdvice: assessmentResult.preventiveAdvice,

        generateTime: new Date().toLocaleString('zh-CN'),

        examinerInfo: {
            name: formData.examinerName || '未填写',
            position: formData.examinerPosition || '未填写',
            institution: formData.examinerInstitution || '未填写'
        }
    };
}

/**
 * 验证口腔检查表单数据
 * @param {Object} formData - 表单数据
 * @returns {Object} 验证结果
 */
export function validateOralForm(formData) {
    const errors = [];
    const warnings = [];

    // 个人信息字段已改为选填，不再进行必填验证
    // 只验证如果填写了年龄，则必须在有效范围内
    if (formData.age !== null && formData.age !== undefined && formData.age !== '') {
        const age = parseInt(formData.age);
        if (isNaN(age) || age < 1 || age > 150) {
            errors.push('年龄应在1-150岁之间');
        }
    }
    
    // 必填字段验证（不包括个人信息字段）
    const requiredFields = {
        examDate: '检查日期',
        caries_present: '龋齿检查',
        defect_present: '牙齿缺损检查',
        missing_present: '牙齿缺失检查',
        gingival_color: '牙龈颜色',
        gingival_texture: '牙龈质地',
        gingival_bleeding: '牙龈出血',
        pocket_depth_normal: '牙周袋深度',
        bone_resorption: '牙槽骨吸收',
        mucosa_normal: '口腔黏膜',
        facial_symmetry_normal: '面部对称性',
        tmj_clicking: '关节弹响',
        tmj_pain: '关节疼痛'
    };

    Object.entries(requiredFields).forEach(([field, label]) => {
        if (!formData[field] && formData[field] !== 0) {
            errors.push(`${label}为必填项`);
        }
    });

    // 数据合理性验证
    if (formData.age && (formData.age < 1 || formData.age > 150)) {
        errors.push('年龄应在1-150岁之间');
    }

    if (formData.mouth_opening && (formData.mouth_opening < 0 || formData.mouth_opening > 80)) {
        errors.push('开口度应在0-80mm之间');
    }

    if (formData.pocket_depth_max && (formData.pocket_depth_max < 0 || formData.pocket_depth_max > 20)) {
        errors.push('牙周袋深度应在0-20mm之间');
    }

    // 条件性字段验证
    if (formData.caries_present === 'yes') {
        if (!formData.caries_details) {
            warnings.push('建议详细描述龋齿情况');
        }
        if (!formData.caries_severity || formData.caries_severity.length === 0) {
            warnings.push('建议选择龋坏程度');
        }
    }

    if (formData.defect_present === 'yes') {
        if (!formData.defect_cause || formData.defect_cause.length === 0) {
            warnings.push('建议选择缺损原因');
        }
        if (!formData.defect_details) {
            warnings.push('建议详细描述缺损情况');
        }
    }

    if (formData.missing_present === 'yes') {
        if (!formData.missing_positions) {
            warnings.push('建议详细记录缺失牙位');
        }
        if (!formData.missing_duration) {
            warnings.push('建议选择缺失时间');
        }
    }

    if (formData.pocket_depth_normal === 'abnormal') {
        if (!formData.pocket_depth_details) {
            warnings.push('建议详细记录异常牙周袋深度');
        }
        if (!formData.pocket_depth_max) {
            warnings.push('建议填写最大牙周袋深度');
        }
    }

    if (formData.mucosa_normal === 'abnormal') {
        if (!formData.mucosa_lesion_type || formData.mucosa_lesion_type.length === 0) {
            warnings.push('建议选择黏膜病变类型');
        }
        if (!formData.mucosa_lesion_details) {
            warnings.push('建议详细描述病变情况');
        }
    }

    return {
        isValid: errors.length === 0,
        errors,
        warnings,
        hasWarnings: warnings.length > 0
    };
}

export default {
    calculateOralAssessment,
    generateOralReport,
    validateOralForm
};
